package com.bybops.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import baseproject.com.mybaseproject.ui.base.BaseActivity
import baseproject.com.mybaseproject.ui.base.BaseFragment
import baseproject.com.mybaseproject.utils.scheduler.RxUtils
import com.bybops.R
import com.bybops.ui.fragment.NewDeviceInitFragment
import com.bybops.ui.fragment.NewDeviceStartUseFragment
import com.bybops.ui.fragment.NewDeviceTestFragment
import kotlinx.android.synthetic.main.activity_new_device_use.*
import kotlinx.android.synthetic.main.toolbar_base.*
import kotlinx.android.synthetic.main.toolbar_base.view.*

/**
 * 启用新设备界面
 */
class NewDeviceUseActivity : BaseActivity() {
    //初始化
    private var initFragment : NewDeviceInitFragment ?= null
    private val TAG_INIT = "TAG_INIT"
    //测试
    private var testFragment : NewDeviceTestFragment ?= null
    private val TAG_TEST = "TAG_TEST"
    //启用
    private var startUseFragment : NewDeviceStartUseFragment ?= null
    private val TAG_START_USE = "TAG_START_USE"

    private var currFragment : BaseFragment ?= null
    private var TAG_CURR = "TAG_CURR"

    //参数
    var mDeviceId = "" // 设备id
    var mLockId = ""  // 蓝牙锁id
    var mHospital = ""  // 医院
    var mAddress = ""  // 位置
    var mDailyMan = ""  // 责任人
    var lockName = ""  // 蓝牙锁的mac地址
    var mVoltage : String ?= null  // 蓝牙锁的电量

    override fun getLayoutId(): Int? {
        return R.layout.activity_new_device_use
    }
    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar?.titleBar(R.id.mToolBar)
                ?.statusBarDarkFont(true, 0.2f)
                ?.navigationBarColor(R.color.colorPrimaryDark)
                ?.init()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {
        mToolBar.mTitle.text = "新设备启用"
        step(0)
        RxUtils.rxClick(mToolBar.mBtnBack , object : RxUtils.OnClickInterf{
            override fun onViewClick(view: View) {
                finish()
            }
        })
    }

    fun step(step : Int){
        when(step){
            0 ->{
                //初始化
                stvInit.leftTextView.setTextColor(resources.getColor(R.color.color_2c9cf0))
                stvInit.leftIconIV.setImageResource(R.mipmap.icon_scan_init_blue)

                stvTest.leftTextView.setTextColor(resources.getColor(R.color.color_333))
                stvTest.leftIconIV.setImageResource(R.mipmap.icon_open_test)

                stvStartUse.leftTextView.setTextColor(resources.getColor(R.color.color_333))
                stvStartUse.leftIconIV.setImageResource(R.mipmap.icon_start_use)
            }
            1 ->{
                //测试
                stvInit.leftTextView.setTextColor(resources.getColor(R.color.color_333))
                stvInit.leftIconIV.setImageResource(R.mipmap.icon_scan_init)

                stvTest.leftTextView.setTextColor(resources.getColor(R.color.color_2c9cf0))
                stvTest.leftIconIV.setImageResource(R.mipmap.icon_open_test_blue)

                stvStartUse.leftTextView.setTextColor(resources.getColor(R.color.color_333))
                stvStartUse.leftIconIV.setImageResource(R.mipmap.icon_start_use)
            }
            2 ->{
                //启用
                stvInit.leftTextView.setTextColor(resources.getColor(R.color.color_333))
                stvInit.leftIconIV.setImageResource(R.mipmap.icon_scan_init)

                stvTest.leftTextView.setTextColor(resources.getColor(R.color.color_333))
                stvTest.leftIconIV.setImageResource(R.mipmap.icon_open_test)

                stvStartUse.leftTextView.setTextColor(resources.getColor(R.color.color_2c9cf0))
                stvStartUse.leftIconIV.setImageResource(R.mipmap.icon_start_use_blue)
            }
        }
        whichFragment(step)
    }

    /**
     * 切换fragment
     */
    private fun whichFragment(step : Int){
        when(step){
            0 ->{
                //初始化
                if (initFragment == null) initFragment = NewDeviceInitFragment()
                currFragment = initFragment
                TAG_CURR = TAG_INIT
            }
            1 ->{
                //测试
                if (testFragment == null) testFragment = NewDeviceTestFragment()
                currFragment = testFragment
                TAG_CURR = TAG_TEST
            }
            2 ->{
                //启用
                if (startUseFragment == null) startUseFragment = NewDeviceStartUseFragment()
                currFragment = startUseFragment
                TAG_CURR = TAG_START_USE
            }
        }
        supportFragmentManager.beginTransaction().replace(R.id.flContent ,currFragment , TAG_CURR).commit()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(currFragment){
            is NewDeviceInitFragment -> initFragment?.onActivityResult(requestCode , resultCode , data)
        }
    }
}